//
//  UIView+ACExt.m
//  ACFramework
//
//  Created by caoyq on 2018/5/23.
//  Updated by caoyq on 2018/6/25.
//

#import "UIView+ACExt.h"

@implementation UIView (ACExt)

#pragma mark - 视图

+ (instancetype)ac_viewFromXIB
{    
    NSString *name = NSStringFromClass(self);
    UIView *xibView=[[[NSBundle mainBundle] loadNibNamed:name owner:nil options:nil] firstObject];
    if(xibView == nil){
        NSLog(@"CoreXibView：从xib创建视图失败，当前类是：%@",name);
    }
    return xibView;
}

- (void)ac_addSubviewsWithArray:(NSArray *)subViews
{
    for (UIView *view in subViews) {
        [self addSubview:view];
    }
}

- (void)ac_removeViews:(NSArray *)views
{
    dispatch_async(dispatch_get_main_queue(), ^{
        for (UIView *view in views) {
            [view removeFromSuperview];
        }
    });
}

- (UIViewController *)ac_viewController
{
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

- (void)ac_setBackgroundImage: (NSString *)imageName ofType: (NSString *)ext
{
    //这种方式设置背景图片能让图片自适应
    NSString *path = [[NSBundle mainBundle] pathForResource:imageName ofType:ext];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    self.layer.contents = (id)image.CGImage;
}

#pragma mark - 添加边框

/**
 添加单条边框
 
 @param direction 边框方向
 @param color     边框颜色
 @param width     边框宽度
 */
- (void)ac_addSingleBorder: (UIViewBorderDirection)direction color: (UIColor *)color width: (CGFloat)width
{
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = color;
    line.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:line];
    
    NSDictionary *views = NSDictionaryOfVariableBindings(line);
    NSDictionary *metrics = @{@"w" : @(width),
                              @"y" : @(self.frame.size.height - width),
                              @"x" : @(self.frame.size.width - width)
                              };
    NSString *vfl_H = @"";
    NSString *vfl_W = @"";
    
    switch (direction) {
        case UIViewBorderDirectionTop:
        {
            vfl_H = @"H:|-0-[line]-0-|";
            vfl_W = @"V:|-0-[line(==w)]";
        }
            break;
        case UIViewBorderDirectionLeft:
        {
            vfl_H = @"H:|-0-[line(==w)]";
            vfl_W = @"V:|-0-[line]-0-|";
        }
            break;
        case UIViewBorderDirectionBottom:
        {
            vfl_H = @"H:|-0-[line]-0-|";
            vfl_W = @"V:[line(==w)]-0-|";
        }
            break;
        case UIViewBorderDirectionRight:
        {
            vfl_H = @"H:|-x-[line(==w)]";
            vfl_W = @"V:|-0-[line]-0-|";
        }
            break;
    }
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl_H options:0 metrics:metrics views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:vfl_W options:0 metrics:metrics views:views]];
}

- (void)ac_addCornerWithDirection: (UIRectCorner)direction radius: (CGFloat)radius
{
    //注意是autolayout布局的情况，不然获取不到self.bounds
    [self layoutIfNeeded];
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:direction cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

#pragma mark - 阴影

- (void)ac_setLayerShadow:(UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius opacity: (CGFloat)opacity {
    //同时设置阴影与圆角是不生效的
    self.layer.shadowColor = color.CGColor;
    self.layer.shadowOffset = offset;
    self.layer.shadowRadius = radius;
    self.layer.shadowOpacity = opacity;
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
}

@end
